package com.example.handheld4_gsignal_app.ui.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.handheld4_gsignal_app.R;
import com.example.handheld4_gsignal_app.data.Task;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * 自定义任务列表适配器 - 支持卡片样式和美观布局
 */
public class TaskListAdapter extends BaseAdapter {

    private Context context;
    private List<Task> tasks;
    private LayoutInflater inflater;
    private SimpleDateFormat dateFormat;

    public TaskListAdapter(Context context, List<Task> tasks) {
        this.context = context;
        this.tasks = tasks;
        this.inflater = LayoutInflater.from(context);
        this.dateFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    }

    @Override
    public int getCount() {
        return tasks != null ? tasks.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return tasks != null ? tasks.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.task_list_item, parent, false);
            holder = new ViewHolder();
            holder.taskIcon = convertView.findViewById(R.id.task_icon);
            holder.taskTitle = convertView.findViewById(R.id.task_title);
            holder.taskStatus = convertView.findViewById(R.id.task_status);
            holder.taskDescription = convertView.findViewById(R.id.task_description);
            holder.taskCreatedTime = convertView.findViewById(R.id.task_created_time);
            holder.taskAssignee = convertView.findViewById(R.id.task_assignee);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Task task = tasks.get(position);
        if (task != null) {
            // 设置任务图标
            holder.taskIcon.setText(getTaskIcon(task.getStatus()));
            
            // 设置任务标题
            holder.taskTitle.setText(task.getTitle());
            
            // 设置任务状态
            holder.taskStatus.setText(task.getStatus());
            holder.taskStatus.setTextColor(getStatusColor(task.getStatus()));
            
            // 设置任务描述
            holder.taskDescription.setText(task.getDescription());
            
            // 设置创建时间
            if (task.getCreatedAt() != null) {
                holder.taskCreatedTime.setText(task.getCreatedAt());
            } else {
                holder.taskCreatedTime.setText("未知时间");
            }
            
            // 设置分配人（如果有的话）
            holder.taskAssignee.setText("管理员");
        }

        return convertView;
    }

    /**
     * 根据任务状态获取对应的图标
     */
    private String getTaskIcon(String status) {
        if (status == null) return "📋";
        
        switch (status) {
            case "待处理":
                return "⏳";
            case "进行中":
                return "🔄";
            case "已完成":
                return "✅";
            case "已取消":
                return "❌";
            default:
                return "📋";
        }
    }

    /**
     * 根据任务状态获取对应的颜色
     */
    private int getStatusColor(String status) {
        if (status == null) return context.getResources().getColor(R.color.text_primary);
        
        switch (status) {
            case "待处理":
                return context.getResources().getColor(R.color.status_pending);
            case "进行中":
                return context.getResources().getColor(R.color.status_progress);
            case "已完成":
                return context.getResources().getColor(R.color.status_completed);
            case "已取消":
                return context.getResources().getColor(R.color.status_cancelled);
            default:
                return context.getResources().getColor(R.color.text_primary);
        }
    }

    /**
     * 更新任务列表数据
     */
    public void updateTasks(List<Task> newTasks) {
        this.tasks = newTasks;
        notifyDataSetChanged();
    }

    /**
     * ViewHolder模式，提高列表性能
     */
    private static class ViewHolder {
        TextView taskIcon;
        TextView taskTitle;
        TextView taskStatus;
        TextView taskDescription;
        TextView taskCreatedTime;
        TextView taskAssignee;
    }
}